var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/Transfer/InfiniteScrollTransfer/index.tsx
var InfiniteScrollTransfer_exports = {};
__export(InfiniteScrollTransfer_exports, {
  default: () => InfiniteScrollTransfer_default
});
module.exports = __toCommonJS(InfiniteScrollTransfer_exports);
var import_hooks = require("@szzj/hooks");
var import_antd = require("antd");
var import_react = require("react");
var import_createField = __toESM(require("../../createField"));
var import_style = require("antd/es/transfer/style/index.js");
var import_react2 = __toESM(require("react"));
var InfiniteScrollTransfer = ({
  request,
  limit,
  formList,
  ...rest
}) => {
  const [dataSource, setDataSource] = (0, import_react.useState)([]);
  const [noMore, setNoMore] = (0, import_react.useState)(false);
  const intervalRef = (0, import_react.useRef)(1);
  const fetchData = (0, import_hooks.useRequest)(
    async ({ currentPage, pageSize = 10 }, options = {}) => {
      var _a, _b;
      const res = await request({
        currentPage,
        pageSize,
        ...options
      });
      if (res == null ? void 0 : res.success) {
        if (!((_a = res.data) == null ? void 0 : _a.list)) {
          setNoMore(true);
          return;
        }
        return ((_b = res.data) == null ? void 0 : _b.list) || [];
      } else {
        return [];
      }
    }
  );
  const handleScroll = async (direction, e) => {
    if (direction === "left" && Math.floor(e.target.scrollHeight - e.target.scrollTop) === e.target.clientHeight && !noMore) {
      intervalRef.current = ++intervalRef.current;
      const res = await (limit ? fetchData.runAsync({
        currentPage: intervalRef.current,
        pageSize: limit
      }) : fetchData.runAsync({ currentPage: intervalRef.current }));
      setDataSource((prevData) => [...prevData, ...res ?? []]);
    }
  };
  async function init() {
    const res = await (limit ? fetchData.runAsync({
      currentPage: intervalRef.current,
      pageSize: limit
    }) : fetchData.runAsync({ currentPage: intervalRef.current }));
    if (formList) {
      setDataSource([...res ?? [], ...formList]);
    } else {
      setDataSource(res);
    }
  }
  (0, import_react.useEffect)(() => {
    init();
  }, []);
  return /* @__PURE__ */ import_react2.default.createElement(
    import_antd.Transfer,
    {
      dataSource,
      render: (item) => item.title,
      onScroll: handleScroll,
      ...rest
    }
  );
};
var InfiniteScrollTransfer_default = (0, import_createField.default)(
  InfiniteScrollTransfer,
  {
    defaultFormItemProps: {
      valuePropName: "targetKeys"
    }
  }
);
